package concurrency.chapter7;

public class TicketWindowRunnable implements Runnable {
    private static final int MAX = 500;
    //static可以实现多个线程访问同一个数据，不会出现重复数据，但生命周期太长
    private static int index = 1;
    private final Object MONITOR = new Object();

    @Override
    public void run() {
        while (true) {
            //为了保证数据一致性synchronized代码块内是单线程
            synchronized (MONITOR) {
                if (index > MAX)
                    break;
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "的号码是：" + index++);
            }
        }
    }
}
